﻿/**
 * The StatusIndicator component displays the status of an event or action using its timeline as the visual indicator. The value of the StatusIndicator will be interpolated with the minimum and maximum values to generate a frame number that will be played in the component’s timeline. Since the component’s timeline is used to display the status, it provides absolute freedom in creating innovative visual indicators.
 
    <b>Inspectable Properties</b>
    A MovieClip that derives from the StatusIndicator component will have the following inspectable properties:
    <ul>
        <li><i>enabled</i>: Disables the component if set to false.</li>
        <li><i>minimum</i>: The minimum value used to interpolate the target frame.</li>
        <li><i>maximum</i>: The maximum value used to interpolate the target frame.</li>
        <li><i>value</i>: The status value of an event or action. It is interpolated between the minimum and maximum values to generate a frame number to be played.</li>
        <li><i>visible</i>: Hides the component if set to false.</li>
    </ul>
    
    <b>States</b>
    There are no states for the StatusIndicator component. The component’s frames are used to display the status of an event or action. 
    
    <b>Events</b>
    All event callbacks receive a single Object parameter that contains relevant information about the event. The following properties are common to all events. <ul>
    <li><i>type</i>: The event type.</li>
    <li><i>target</i>: The target that generated the event.</li></ul>
        
    No special events are generated by the StatusIndicator. The properties listed next to the event are provided in addition to the common properties.<ul>
    <li><i>ComponentEvent.SHOW</i>: The visible property has been set to true at runtime.</li>
    <li><i>ComponentEvent.HIDE</i>: The visible property has been set to false at runtime.</li>
 */

/**************************************************************************

Filename    :   StatusIndicator.as

Copyright   :   Copyright 2011 Autodesk, Inc. All Rights reserved.

Use of this software is subject to the terms of the Autodesk license
agreement provided at the time of installation or download, or which
otherwise accompanies this software in either electronic or hard copy form.

**************************************************************************/
 
package scaleform.clik.controls {
    
    import scaleform.clik.core.UIComponent;
    import scaleform.clik.constants.InvalidationType;
    import scaleform.clik.constants.ScrollBarDirection;
    
    public class StatusIndicator extends UIComponent {
        
    // Constants:
        
    // Public Properties:
    
    // Protected Properties:
        protected var _maximum:Number = 10;
        protected var _minimum:Number = 0;
        protected var _value:Number = 0;
        
    // UI Elements:
        
    // Initialization:
        public function StatusIndicator() {
            super();
        }
        
        override protected function initialize():void {
            super.initialize();
        }
        
    // Public getter / setters:
        /**
         * The maximum number the {@code value} can be set to.
         */
        [Inspectable(defaultValue = "10")]
        public function get maximum():Number { return _maximum; }
        public function set maximum(value:Number):void { 
            _maximum = value; 
        }
        
        /**
         * The minimum number the {@code value} can be set to.
         */
        [Inspectable(defaultValue="0")]
        public function get minimum():Number { return _minimum; }
        public function set minimum(value:Number):void { 
            if (_minimum == value) { return; }
            _minimum = value;
            updatePosition();
        }
        
        /**
         * The current value between the {@code maximum} and {@code minimum}.
         * @see #maximum
         * @see #minimum
         */
        [Inspectable(defaultValue="0")]
        public function get value():Number { return _value; }
        public function set value(value:Number):void {
            var range:Number = Math.max(_minimum, Math.min(_maximum, value));
            if (_value == range) { return; } 
            _value = range;
            updatePosition();
        }
        
        /**
         * The {@code value} of the {@code StatusIndicator}, to make it polymorphic with a {@link ScrollIndicator}.
         */
        public function get position():Number { return _value; }
        public function set position(value:Number):void { this.value = value; }
        
    // Public Methods:
        /** @exclude */
        override public function toString():String {
            return "[CLIK StatusIndicator " + name + "]";
        }
    
    // Private Methods:
        override protected function configUI():void {
            super.configUI();
            tabEnabled = focusable = false;
        }
        
        override protected function draw():void {
            if (isInvalid(InvalidationType.SIZE)) {
                setActualSize(_width, _height);
            }
            
            if (isInvalid(InvalidationType.DATA)) {
                updatePosition();
            }
        }
        
        // This is the default behaviour. which can be changed by overriding this method, or defining it in the timeline.
        protected function updatePosition():void {
            if (!enabled) { return; }
            var percent:Number = (_value - _minimum) / (_maximum - _minimum);
            gotoAndStop(Math.max(1, Math.round(percent * totalFrames)));
        }
    }
}